<template>
  <div>
    <el-input
      v-model="myvalue"
      :placeholder="placeholder"
    >
      <template slot="append">
        <el-button 
          size="small"
          type="danger"
          @click="onok"
        > 确定</el-button>
      </template>
    </el-input>
  
    <el-dialog
     :append-to-body="true"
      title="选择数据"
      :close-on-click-modal="false"
      :visible="showDialog"
      @close="onclosedlg"
    >
      <div>{{this.cate}}</div>
      <el-table
        :data="datas"
         @current-change="handleSelectionChange"
        v-if="cate == '学号'"
      >
        <el-table-column
          label="选择"
          width="65"
        >
        
          <template slot-scope="scope">
            <el-checkbox v-model="scope.row.checked"></el-checkbox> 
          </template>
     
        </el-table-column>
        <el-table-column
          align="center"
          prop="BJMC"
          label="班级"
        />
        <el-table-column
          align="center"
          prop="XSXH"
          label="学号"
        />
        <el-table-column
          align="center"
          prop="XM"
          label="姓名"
        />
      </el-table>
      
      <el-table
       @current-change="handleSelectionChange"
        :data="datas"
        v-if="cate == '教工号'"
      >
      <el-table-column
          label="选择"
          width="65"
        >
         
          <template slot-scope="scope">
            <el-checkbox v-model="scope.row.checked"></el-checkbox> 
          </template>
      
        </el-table-column>
        <el-table-column
          align="center"
          prop="BMMC"
          label="部门"
        />
        <el-table-column
          align="center"
          prop="GH"
          label="工号"
        />
        <el-table-column
          align="center"
          prop="XM"
          label="姓名"
        />
      </el-table>
       <el-table
       @current-change="handleSelectionChange"
        :data="datas"
        v-if="cate == '课程名称'"
      >
      <el-table-column
          label="选择"
          width="65"
        >
        
          <template slot-scope="scope">
            <el-checkbox v-model="scope.row.checked"></el-checkbox> 
          </template>
      
        </el-table-column>
        <el-table-column
          align="center"
          prop="BMMC"
          label="开课学院"
        />
        <el-table-column
          align="center"
          prop="ZYMC"
          label="专业名称"
        />
          <el-table-column
          align="center"
          prop="ZYFXMC"
          label="专业方向名称"
        />
         <el-table-column
          align="center"
          prop="KCMC"
          label="课程名称"
        />
      </el-table>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="cancel"
        >取消</el-button>
        <el-button
          type="primary"
          @click="ok"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
import linq from "linq";
import {
  get在校生信息,
  get在校生信息ByXSXH,
  search在校生信息
} from "@/api/在校生信息";
import { search教师基本情况, get教师基本情况ByGH } from "@/api/教师基本情况";
import { search课程设置 } from "@/api/课程设置";
import { search机构设置, get机构设置ByGH } from "@/api/机构设置";
import { showLoading, hideLoading } from "@/utils/loading";
export default {
  props: ["description", "cate", "value"],
  data() {
    return {
      templateRadio:'',
      myvalue: "",
      datas:null,
      placeholder:"",
      showDialog: false
    };
  },
  async created() {
  
    this.myvalue = this.value;
    if(this.cate =='学号'){
      this.placeholder = '请输入学号或姓名点击确定进行模糊搜索'
    }
    if(this.cate =='教工号'){
      this.placeholder = '请输入工号或姓名点击确定进行模糊搜索'
    }
    
    if(this.cate =='课程名称'){
      this.placeholder = '请输入课程名点击确定进行模糊搜索'
    }
    
  },
  methods: {
    onclosedlg() {
      this.showDialog = false;
      
    },
    cancel(){
      this.showDialog=false;
    },
    ok(){
    
      if(this.cate =='教工号'){
        var  teacher =linq.from(this.datas).first("$.checked==true");
          this.myvalue = teacher.GH;
       this.$emit("input", teacher.GH);
       this.$emit("input", teacher.GH);
       this.showDialog=false;
      }
       if(this.cate =='学号'){
        
        var  student =linq.from(this.datas).first("$.checked==true");
         this.myvalue = student.XSXH;
         this.$emit("input", student.XSXH);
          this.$emit("input", student.XSXH);
      
       this.showDialog=false;
      }
      if(this.cate =='课程名称'){
        
        var  kc  =linq.from(this.datas).first("$.checked==true");
          this.myvalue =  kc.KCMC;
        //  this.$emit("input", kc.ID);
        //  this.$emit("input", kc.ID);
        this.$emit("input", kc.KCMC);
          this.$root.eventHub.$emit("changeKcsz",kc)
         this.showDialog=false;
      }
    },
    handleSelectionChange (row) {
      
      if(row){
        this.datas.forEach(item => {
          // 排他,每次选择时把其他选项都清除
          if (item.ID !== row.ID) {
            item.checked = false
          }
        })
    }
    },

    async onok() {
      if (this.myvalue.trim() == "") {
        this.$alert("请输入学号或教工号");
        return;
      }
      if(this.cate=="课程名称")
      {
         showLoading();

        var kcs = await search课程设置(
          this.$root.user.XXID,
          this.$root.user.CJPC,
          this.myvalue
        );
      
        hideLoading();
        
        if (kcs.length == 0) {
          this.$alert("没有找到对应课程号或课程名的课程");
          this.$emit("input", "error");
          this.$emit("input", "error");
        } else if (kcs.length > 1) {
          kcs.forEach(item => {
          item.checked = false
        })
          this.datas = kcs;
          this.showDialog=true;
        } else {
          
               this.myvalue =  kcs[0].KCMC;
                
              this.$emit("input", kcs[0].KCMC);
             this.$root.eventHub.$emit("changeKcsz",kcs[0])
           
        }
      }
      if (this.cate == "教工号") {
        //
        showLoading();

        var teachers = await search教师基本情况(
          this.$root.user.XXID,
          this.$root.user.CJPC,
          this.myvalue
        );
        hideLoading();
        if (teachers.length == 0) {
          this.$alert("没有找到对应教工号的教师");
          this.$emit("input", "error");
          this.$emit("input", "error");
        } else if (teachers.length > 1) {
          teachers.forEach(item => {
          item.checked = false
        })
          this.datas = teachers;
          this.showDialog=true;
        } else {
               this.myvalue =  teachers[0].GH;
          this.$emit("input", teachers[0].GH);
          this.$emit("input", teachers[0].GH);
        }
      }
      if (this.cate == "学号") {
        //
        showLoading();
        var students = await get在校生信息ByXSXH(
          this.$root.user.XXID,
          this.$root.user.CJPC,
          this.myvalue
        );
       
        hideLoading();
        
        if (students.length == 0) {
          this.$alert("没有找到对学号的学生");
          this.$emit("input", "error");
          this.$emit("input", "error");
        } else if (students.length > 1) {
          
          this.datas = students;
            students.forEach(item => {
          item.checked = false
        })
          this.showDialog=true;
        }
         else {
         
               this.myvalue = students[0].XSXH
          
          this.$emit("input", students[0].XSXH);
          this.$emit("input", students[0].XSXH);
        }
      }
    }
  }
};
</script>
